﻿// -----------------------------------------------------------------------
// <copyright file="DateTimeCalculations.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AwesomeUtils
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class DateTimeCalculations
    {
        /// <summary>
        /// For a 30 day billing cycle gets start date accounting for days 29-30 by advancing start day to the 1st of the next month.
        /// </summary>
        public static DateTime getBillingStartDate
        {
            get
            {
                DateTime result = DateTime.Now;
                if (result.Day > 28) result = new DateTime(result.Year, result.AddMonths(1).Month, 1);
                return result;
            }
        }

        public static int CalculateAge(DateTime birthdate)
        {
            // get the difference in years
            int years = DateTime.Now.Year - birthdate.Year;
            // subtract another year if we're before the birth day in the current year
            if (DateTime.Now.Month < birthdate.Month || (DateTime.Now.Month == birthdate.Month && DateTime.Now.Day < birthdate.Day))
                years--;

            return years;
        }

        public static TimeSpan ElapsedTime(DateTime PastDate)
        {
            return ElapsedTime(PastDate, DateTime.Now);
        }
        public static TimeSpan ElapsedTime(DateTime PastDate, DateTime FutureDate)
        {
            TimeSpan ts;
            DateTime dt1 = FutureDate;
            DateTime dt2 = PastDate;
            ts = dt1.Subtract(dt2);
            return ts;
        }

        public static double getProRatedCredit(DateTime StartDate, DateTime EndDate, double PackageAmount)
        {
            Double result = 0;
            if (EndDate > DateTime.Now)
            {
                double TotalDays = ElapsedTime(StartDate, EndDate).TotalDays;
                double dailyrate = PackageAmount / TotalDays;

                double DaysLeft = ElapsedTime(DateTime.Now, EndDate).TotalDays;
                result = DaysLeft * dailyrate;
            }
            return result;
        }

        public static string getPostedTime(Object PastDate)
        {
            return string.Empty;
            TimeSpan ts = ElapsedTime(Convert.ToDateTime(PastDate));
            string result = string.Empty;
            if (ts.TotalMinutes < 1) result = string.Format("{0} seconds", ts.TotalSeconds < 2 ? 2 : Convert.ToInt16(ts.TotalSeconds));
            else if (ts.TotalHours < 1) result = string.Format("{0} minutes", Convert.ToInt16(ts.TotalMinutes));
            else if (ts.TotalDays <= 1) result = string.Format("about {0} hours", Convert.ToInt16(ts.TotalHours));
            else result = string.Format("about {0} days", Convert.ToInt16(ts.TotalDays));
            //else if (ts.TotalDays > 1 && ts.TotalDays <= 7) result = string.Format("about {0} week", Convert.ToInt16(ts.TotalDays / 7));
            //else if (ts.TotalDays <= 60) result = string.Format("about {0} weeks", Convert.ToInt16(ts.TotalDays/7));
            //else result = string.Format("about {0} months", Convert.ToInt16(ts.TotalDays/30));
            return string.Format("Posted {0} ago", result);
        }
    }
}
